<template>
  <div class="login">
    <!-- 参考：https://element.eleme.cn/#/zh-CN/component/form 对齐方式 -->
    <!-- {{ $store.state.count }} -->
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      label-position="top"
    >
      <el-form-item label="账号" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <!-- 属性的配置。属性值与输入框的值-->
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <el-input v-model="form.code" type="text"></el-input>
      </el-form-item>
      <el-form-item>
        <!--  :loading 加载中 避免重复点击。-->
        <el-button type="primary" @click="onSubmit" :loading="isLoginLoading"
          >登录</el-button
        >
        <el-button type="second">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import request from "@/utils/request";
// import qs from "qs";

//  user.js中通过export导出的功能login,如果接别的功能逗号加后面就行
import { login } from "@/services/user";

export default {
  // 导出当前组件的配置对象
  name: "Login",
  data() {
    return {
      form: {
        phone: "",
        password: "",
        code: "",
      },
      rules: {
        phone: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 5,
            max: 50,
            message: "长度在 5 到 50 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          {
            min: 5,
            max: 50,
            message: "长度在 5 到 50 个字符",
            trigger: "change",
          },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "请输入正确的验证码",
            trigger: "blur",
          },
        ],
      },
      isLoginLoading: false,
    };
  },
  methods: {
    // 点击提交的时候对数据进行校验。
    // onSubmit() {
    //   // 括号里面的是回调函数。和java里面的回调的区别，？
    //   this.$refs['form'].validate(vaid => {
    //     console.log(vaid);
    //   })
    // },

    async onSubmit() {
      // commit就是触发指定的方法.
      // this.$store.commit('increment');

      // this.$store.commit("increment", {
      //   amount: 9,
      // });

      // this.$store.dispatch("incrementHandler", {
      //   amount: 11,
      //   delay: 20000,
      // });

      // 不需要回调函数。
      // 浏览器控制台会报错。需要使用try catch进行处理
      try {
        await this.$refs["form"].validate();
        console.log("通过了校验！");
        // this.isLoginLoading = true;
        // 获取响应的信息。
        // const result = await request({
        //   method: "POST",
        //   url: "/front/user/login",
        //   data: qs.stringify(this.form),
        // });
        const result = await login(this.form);
        console.log(result);
        // this.isLoginLoading = false;
        if (result.data.state === 1) {
          this.$store.commit("setUser", result.data.content);
          this.$router.push(this.$route.query.redirect || "/home");
        } else {
          this.$message.error(result.data.message + "登录失败");
        }
      } catch (err) {
        console.log("检验失败！" + err);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  height: 100vh;
  display: flex;
  // 设置内部元素垂直水平居中。
  justify-content: center;
  align-items: center;
  .el-form {
    background-color: #fff;
    padding: 20px;
    width: 300px;
    border-radius: 10px;
    .el-button {
      width: 48%;
    }
  }
}
</style>
